package com.hk.yajin.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.hk.developmentlib.view.itemdecoration.BaseItemBottomDecoration;
import com.hk.yajin.R;
import com.hk.yajin.model.LunBoData;

import org.xutils.common.util.LogUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 伦哥哥 on 2017/6/24.
 */

public abstract class SingleItemListFragment<T> extends BaseFragment {
    protected List<LunBoData> headviewDatas = new ArrayList<>();
    protected View headview;
    protected List<T> dataList = new ArrayList<>();
    protected RecyclerView mRecyclerView;
    protected BaseQuickAdapter mQuickAdapter;
    protected SwipeRefreshLayout swipe_container;
    protected int page = 1;
    protected int pageSize = 10;
    protected boolean loadFinish = true;

    public abstract void initDataList();
    /*轮播相关*/


    protected View initView(LayoutInflater inflater, ViewGroup container) {
        mContentView = inflater.inflate(R.layout.fragment_single_list, container, false);
        initSwipeView();
        initAdapter();
        initRecyclerView();
        return mContentView;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!isCreated) {
            isNetWorkConnect();
            initDataList();
            initLunBo();
            isCreated = true;
        }

    }

    protected void initLunBo() {

    }


    protected void initSwipeView() {
        swipe_container = (SwipeRefreshLayout) mContentView.findViewById(R.id.swipe_container);
        swipe_container.setEnabled(true);
        swipe_container.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mQuickAdapter.removeAllFooterView();
                loadFinish = true;
                initDataList();
                initLunBo();
            }
        });

    }


    protected void initAdapter() {
        //  mQuickAdapter = new SingleItemQuickAdapter(dataList, mContext);
        mQuickAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mQuickAdapter.isFirstOnly(true);
        mQuickAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                initDataList();
            }
        }, mRecyclerView);
        // mQuickAdapter.addHeaderView(getActivity().getLayoutInflater().inflate(R.layout.head_view, (ViewGroup) mRecyclerView.getParent(), false));

    }

    protected void initRecyclerView() {
        mRecyclerView = (RecyclerView) mContentView.findViewById(R.id.rv_list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
//        mQuickAdapter.setLoadingView(getActivity().getLayoutInflater().inflate(R.layout.def_loading, (ViewGroup) mRecyclerView.getParent(), false));
//        mQuickAdapter.setLoadMoreFailedView(getActivity().getLayoutInflater().inflate(R.layout.not_loading, (ViewGroup) mRecyclerView.getParent(), false));
//
        mQuickAdapter.setPreLoadNumber(pageSize / 3);
        setItemDecoration();
        mRecyclerView.setAdapter(mQuickAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                //showShortToast("click:" + position);
            }
        });

    }

    protected void setItemDecoration() {
        mRecyclerView.addItemDecoration(new BaseItemBottomDecoration(getActivity(), 5));
    }

    protected void showEmptView() {
        View view = getActivity().getLayoutInflater().inflate(R.layout.list_empty_view, (ViewGroup) mRecyclerView.getParent(), false);
        mQuickAdapter.setEmptyView(view);
    }

    @Override
    public void defaultViewClick(int tag) {

        hideDefaultView();
        initDataList();

    }

    protected void handleData(final List<T> list) {
        if (list != null) {
            if (list.size() == 0 && page == 1) {//第一次加载，且无数据
                // showEmptView();
                showDefaultView_NO_DTTA();
                mQuickAdapter.loadMoreEnd();
                mQuickAdapter.notifyDataSetChanged();
                loadFinish = true;
                return;
            } else if (list.size() < pageSize) {
                if (page == 1) {
                    dataList.addAll(list);
                    mQuickAdapter.setNewData(dataList);
                    mQuickAdapter.loadMoreEnd();

                } else {
                    dataList.addAll(list);
                    mQuickAdapter.loadMoreEnd();

                }
            } else if (list.size() == pageSize && page == 1) {//首次加载
                //          showSnackBar();
                LogUtil.d("首次加载完成");
                dataList.addAll(list);
                mQuickAdapter.setNewData(dataList);
                mQuickAdapter.loadMoreComplete();
            } else if (list.size() == pageSize && page != 1) {//继续加载，还有数据
                mQuickAdapter.addData(list);
                mQuickAdapter.loadMoreComplete();
            }
            page++;
            loadFinish = true;
        }

    }
}
